//
//  NSDictionary+json.m
//  SeniorCarRepairing
//
//  Created by LYX on 16/9/29.
//  Copyright © 2016年 LYX. All rights reserved.
//

#import "NSDictionary+json.h"

@implementation NSDictionary (json)

- (NSString *)descriptionWithLocale:(id)locale
{
    NSArray *allKeys = [self allKeys];
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"{\t\n "];
    for (NSString *key in allKeys) {
        id value= self[key];
        [str appendFormat:@"\t \"%@\" = %@,\n",key, value];
        }
    [str appendString:@"}"];
    
    return str;
}

-(NSString*)toJsonString
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] && error == nil){
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }else{
        return nil;
    }
}

@end
